package com.ruoyi.wms.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.mapper.WmsWarehouseAreaMapper;
import com.ruoyi.wms.domain.WmsWarehouseArea;
import com.ruoyi.wms.service.IWmsWarehouseAreaService;

/**
 * 库区信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@Service
public class WmsWarehouseAreaServiceImpl implements IWmsWarehouseAreaService 
{
    @Autowired
    private WmsWarehouseAreaMapper wmsWarehouseAreaMapper;

    /**
     * 查询库区信息
     * 
     * @param id 库区信息主键
     * @return 库区信息
     */
    @Override
    public WmsWarehouseArea selectWmsWarehouseAreaById(Long id)
    {
        return wmsWarehouseAreaMapper.selectWmsWarehouseAreaById(id);
    }

    /**
     * 查询库区信息列表
     * 
     * @param wmsWarehouseArea 库区信息
     * @return 库区信息
     */
    @Override
    public List<WmsWarehouseArea> selectWmsWarehouseAreaList(WmsWarehouseArea wmsWarehouseArea)
    {
        // 设置租户ID
        wmsWarehouseArea.setTenantId(SecurityUtils.getTenantId());
        return wmsWarehouseAreaMapper.selectWmsWarehouseAreaList(wmsWarehouseArea);
    }

    /**
     * 新增库区信息
     * 
     * @param wmsWarehouseArea 库区信息
     * @return 结果
     */
    @Override
    public int insertWmsWarehouseArea(WmsWarehouseArea wmsWarehouseArea)
    {
        wmsWarehouseArea.setTenantId(SecurityUtils.getTenantId());
        wmsWarehouseArea.setCreateBy(SecurityUtils.getUsername());
        wmsWarehouseArea.setCreateTime(DateUtils.getNowDate());
        return wmsWarehouseAreaMapper.insertWmsWarehouseArea(wmsWarehouseArea);
    }

    /**
     * 修改库区信息
     * 
     * @param wmsWarehouseArea 库区信息
     * @return 结果
     */
    @Override
    public int updateWmsWarehouseArea(WmsWarehouseArea wmsWarehouseArea)
    {
        wmsWarehouseArea.setUpdateBy(SecurityUtils.getUsername());
        wmsWarehouseArea.setUpdateTime(DateUtils.getNowDate());
        return wmsWarehouseAreaMapper.updateWmsWarehouseArea(wmsWarehouseArea);
    }

    /**
     * 批量删除库区信息
     * 
     * @param ids 需要删除的库区信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsWarehouseAreaByIds(Long[] ids)
    {
        return wmsWarehouseAreaMapper.deleteWmsWarehouseAreaByIds(ids);
    }

    /**
     * 删除库区信息信息
     * 
     * @param id 库区信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsWarehouseAreaById(Long id)
    {
        return wmsWarehouseAreaMapper.deleteWmsWarehouseAreaById(id);
    }

    /**
     * 根据库区编码查询库区信息
     * 
     * @param areaCode 库区编码
     * @return 库区信息
     */
    @Override
    public WmsWarehouseArea selectWmsWarehouseAreaByCode(String areaCode)
    {
        return wmsWarehouseAreaMapper.selectWmsWarehouseAreaByCode(areaCode, SecurityUtils.getTenantId());
    }

    /**
     * 根据仓库ID查询库区列表
     * 
     * @param warehouseId 仓库ID
     * @return 库区信息集合
     */
    @Override
    public List<WmsWarehouseArea> selectWmsWarehouseAreaByWarehouseId(Long warehouseId)
    {
        return wmsWarehouseAreaMapper.selectWmsWarehouseAreaByWarehouseId(warehouseId, SecurityUtils.getTenantId());
    }

    /**
     * 校验库区编码是否唯一
     * 
     * @param wmsWarehouseArea 库区信息
     * @return 结果
     */
    @Override
    public boolean checkAreaCodeUnique(WmsWarehouseArea wmsWarehouseArea)
    {
        Long areaId = StringUtils.isNull(wmsWarehouseArea.getId()) ? -1L : wmsWarehouseArea.getId();
        WmsWarehouseArea info = wmsWarehouseAreaMapper.selectWmsWarehouseAreaByCode(wmsWarehouseArea.getAreaCode(), SecurityUtils.getTenantId());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != areaId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}